package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.mapper.MenuMapper;
import com.indusfo.spc.pojo.Menu;
import com.indusfo.spc.pojo.User;
import com.indusfo.spc.service.MenuService;
import com.indusfo.spc.vo.JSONObject;
import com.indusfo.spc.vo.SimpleTreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单清单信息表业务层
 * @author 贺闻博
 * 根据业务需要  ,此业务层只需要进行查询所有的菜单的清单, 其他的查询的接口待定
 *   2018年11月30日
 */
@Service
public class MenuServiceImpl implements MenuService{
	private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
     @Resource
     private MenuMapper menuMapper;
     
	@Override
	public JSONObject selectMenuS(String vcMenuPar) {
		List<Menu> listMenu =null;
		try {
 			listMenu =menuMapper.selectMenus(vcMenuPar);
 			return  JSONObject.oK("查询成功", listMenu, listMenu.size());
		} catch (GlobalException e) {
			logger.error(e.getMessage(),e);
			return JSONObject.build(500, e.getMessage());
		}
	}
      /**
       * 新增跟新接口暂留
       */
	@Override
	public JSONObject updateMenu(Menu menu) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 之前的接口设置为要为所有的角色和用户设置对应的菜单信息,
	 * 但是为了让超级管理员始终能够得到菜单展示的最高权限,
	 * 所以
	 * @see com.indusfo.spc.service.MenuService#getMenuByUserId(Integer)
	 * @author hewb
	 * @date 2018年12月13日,下午1:45:07
	 */
	@Override
	public Map<String,List<Menu>> getMenuByUserId(Integer lUserId){
		List<Menu>  menuList =null;
		List<Menu> permissList = new ArrayList<>();
		Map<Integer, Menu> map = new HashMap<>();
		     menuList = menuMapper.slectMenuByUserId(lUserId);
		   //在结果集中筛选目录
		   for(Menu menu:menuList){
			   if(menu.getMenuTypeId() != 3 && "00".equals(menu.getMenuPcode()) ){
				     map.put(menu.getMenuId(), menu);
				     permissList.add(menu);
			   }

		   }
		   //将菜单封装
		   for(Menu menu:menuList){
			   if(menu.getMenuTypeId() != 3 && map.get(menu.getMenuPcode())!=null ){
				    Menu menuS = map.get(menu.getMenuPcode());
				       menuS.getChildren().add(menu);

			   }
		   }
		   Map<String, List<Menu>> resultMap = new HashMap<>(2);
		      //获取菜单和按钮
		      resultMap.put("menuList", menuList);
		      //获取菜单
		      resultMap.put("permissList", permissList);

		    return resultMap;

	}
	/**
	 * 获取角色菜单
	 * @see com.indusfo.spc.service.MenuService#selMenus(com.indusfo.spc.pojo.User)
	 * @author hewb
	 * @date 2018年12月14日,上午10:17:18
	 */
	@Override
	public List<Menu> selMenus(User user){
		List<Menu> result = new ArrayList<>();
          Integer userId=user.getUserId();
		 List<Menu> menuAll=null;
		  if(userId==1){
		    menuAll= menuMapper.selectMenuAll();
		  }else{
			menuAll=  menuMapper.slectMenuByUserId(userId);
		  }
		  if(!menuAll.isEmpty()){
		  for(int i = 0; i<menuAll.size();i++){
			  if("00".equals(menuAll.get(i).getMenuPcode())){
				  Menu menus1 = new Menu();
				  menus1.setMenuId(menuAll.get(i).getMenuId());
				  menus1.setMenuCode(menuAll.get(i).getMenuCode());
				  menus1.setMenuName(menuAll.get(i).getMenuName());
				  menus1.setTranAdress(menuAll.get(i).getTranAdress());
				  menus1.setMenuArg(menuAll.get(i).getMenuArg());
				  menus1.setMenuTypeId(menuAll.get(i).getMenuTypeId());
				  menus1.setMenuPcode(menuAll.get(i).getMenuPcode());
				  menus1.setRemark(menuAll.get(i).getRemark());
				  menus1.setChecked("false");
			  List<Menu> menus2 = new ArrayList<>();
			  for(int j= 0 ;j <menuAll.size();j++){
				  if(menuAll.get(j).getMenuPcode().equals(menuAll.get(i).getMenuCode())){
					  Menu menu2 = new Menu();
					  menu2.setMenuId(menuAll.get(j).getMenuId());
					  menu2.setMenuCode(menuAll.get(j).getMenuCode());
					  menu2.setMenuName(menuAll.get(j).getMenuName());
					  menu2.setTranAdress(menuAll.get(j).getTranAdress());
					  menu2.setMenuArg(menuAll.get(j).getMenuArg());
					  menu2.setMenuTypeId(menuAll.get(j).getMenuTypeId());
					  menu2.setMenuPcode(menuAll.get(j).getMenuPcode());
					  menu2.setRemark(menuAll.get(j).getRemark());
					  menu2.setChecked("false");
					  menus2.add(menu2);
				  }
			  }
			  
			  menus1.setChildren(menus2);
			  result.add(menus1);
			  }
		  }
		  }
		return result;
	}


	//---------------------------------- beigin --------------------------------------//

	@Override
	public List<SimpleTreeNode> getMenuTree(List<String> menuRight) {
		// 查询菜单列表
		List<Menu> menuList = menuMapper.selectMenuAll();
		// 将菜单列表转换为树形结构
		List<SimpleTreeNode> menuVoList = menuList.stream().map(menu -> {
			SimpleTreeNode simpleTreeNode = new SimpleTreeNode();
			simpleTreeNode.setId(menu.getMenuId());
			simpleTreeNode.setpCode(menu.getMenuPcode());
			simpleTreeNode.setTitle(menu.getMenuName());
			simpleTreeNode.setCode(menu.getMenuCode());
			return simpleTreeNode;
		}).collect(Collectors.toList());
		//2.4 获取菜单权限树
		List<SimpleTreeNode> menuTree = getTree(menuVoList, "00", menuRight);
		return menuTree;
	}

	/**
	 * 获取菜单权限树
	 *
	 * @author xuz
	 * @date 2019/7/15 12:36 PM
	 * @param menuVoList , vcMenuCode, menuRight]
	 * @return java.util.List<com.indusfo.spc.pojo.SimpleTreeNode>
	 */
	private List<SimpleTreeNode> getTree(List<SimpleTreeNode> menuVoList, String vcMenuCode, List<String> menuRight) {
		List<SimpleTreeNode> menuVoNodes = new ArrayList<>();
		for (Iterator<SimpleTreeNode> iterator = menuVoList.iterator(); iterator.hasNext();) {
			SimpleTreeNode treeNode = iterator.next();
			String vcMenuCode1 = treeNode.getCode();
			String vcMenuPar = treeNode.getpCode();
			// 数据表中，编码长度为2的都为父级菜单，大于2的为子级菜单
			if ((null!=vcMenuCode1 && "00".equals(vcMenuCode1)) || (null!=vcMenuPar && null!=vcMenuCode && vcMenuPar.equals(vcMenuCode))) {
				treeNode.setChildren(getTree(menuVoList, vcMenuCode1, menuRight));
				// 如果存在与角色菜单权限中，则设置为选择
				if (null != menuRight && menuRight.contains(vcMenuCode1)) {
					// 只有最后一子级才设置为true，前端VUE框架
					List<SimpleTreeNode> children = treeNode.getChildren();
					if (null==children || children.size()==0) {
						treeNode.setChecked(true);
					}
				}
				menuVoNodes.add(treeNode);
			}
		}
		return menuVoNodes;
	}

	//---------------------------------- end --------------------------------------//

}
